﻿using HtmlAgilityPack;
using System;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;

namespace ZingFrameworkCore.Common.Helpers
{
    public static class StringHelper
    {
        /// <summary>
        /// GUID
        /// </summary>
        /// <returns></returns>
        public static string GuId()
        {
            return Guid.NewGuid().ToString();
        }
        /// <summary>
        /// 序列号
        /// </summary>
        /// <returns></returns>
        public static string AutoSerialNumber()
        {
            var random = new Random();
            var numbers = random.Next(1000, 10000).ToString();
            return $"{DateTime.Now.ToString("yyyyMMddHHmmss")}{numbers}";
        }
        /// <summary>
        /// 清理HTML中的样式
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string ClearHtmlTagStyle(this string input)
        {
            if (string.IsNullOrEmpty(input))
            {
                return "";
            }
            var htmlDcoument = new HtmlDocument();
            htmlDcoument.LoadHtml(input);
            htmlDcoument.DocumentNode.Descendants().Where(x => x.Attributes.Contains("style") || x.Attributes.Contains("class")).ToList().ForEach((p) =>
            {
                if (p.Attributes["style"] != null)
                {
                    p.Attributes["style"].Remove();
                }
                if (p.Attributes["class"] != null)
                {
                    p.Attributes["class"].Remove();
                }
            });
            return htmlDcoument.DocumentNode.InnerHtml;
        }
        /// <summary>
        /// 清理HTML代码
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string ClearHtml(this string input)
        {
            if (string.IsNullOrWhiteSpace(input))
            {
                return "";
            }
            var Htmlstring = input;
            //删除脚本
            Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "",
            RegexOptions.IgnoreCase);
            //删除HTML 
            Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "",
            RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "",
            RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"–>", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"<!–.*", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"",
            RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&",
            RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<",
            RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">",
            RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", "   ",
            RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);
            Htmlstring.Replace("<", "");
            Htmlstring.Replace(">", "");
            Htmlstring.Replace("\r\n", "");
            Htmlstring = WebUtility.HtmlEncode(Htmlstring).Trim();
            return Htmlstring;
        }
        /// <summary>
        /// 格式化文本（防止SQL注入）
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string InjectFormat(this string input)
        {
            var regex1 = new Regex(@"<script[\s\S]+</script *>", RegexOptions.IgnoreCase);
            var regex2 = new Regex(@" href *= *[\s\S]*script *:", RegexOptions.IgnoreCase);
            var regex3 = new Regex(@" on[\s\S]*=", RegexOptions.IgnoreCase);
            var regex4 = new Regex(@"<iframe[\s\S]+</iframe *>", RegexOptions.IgnoreCase);
            var regex5 = new Regex(@"<frameset[\s\S]+</frameset *>", RegexOptions.IgnoreCase);
            var regex10 = new Regex(@"select", RegexOptions.IgnoreCase);
            var regex11 = new Regex(@"update", RegexOptions.IgnoreCase);
            var regex12 = new Regex(@"delete", RegexOptions.IgnoreCase);

            input = regex1.Replace(input, ""); //过滤<script></script>标记
            input = regex2.Replace(input, ""); //过滤href=javascript: (<A>) 属性
            input = regex3.Replace(input, " _disibledevent="); //过滤其它控件的on...事件
            input = regex4.Replace(input, ""); //过滤iframe
            input = regex10.Replace(input, "s_elect");
            input = regex11.Replace(input, "u_pudate");
            input = regex12.Replace(input, "d_elete");
            input = input.Replace("'", "’");
            input = input.Replace("&nbsp;", " ");
            return input;
        }
        /// <summary>
        /// 去除空格
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string ToTrim(this string value)
        {
            if (string.IsNullOrEmpty(value)) return string.Empty;
            var strs = value.Trim().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            return string.Join(" ", strs);
        }

    }
}
